﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KOPLibrary.AutoTemplateDesign;
namespace KOPLibrary.Forms
{
    public partial class StyleManager : Form
    {
        public TextStyleList StyleList;
        public StyleManager(TextStyleList stylelist)
        {
            StyleList = stylelist;
            InitializeComponent();
            InıtUI();
        }

        private void InıtUI()
        {
            foreach (FontFamily item in FontFamily.Families)
            {
                ctlFontName.Items.Add(item.Name);
            }
            ctlFontName.SelectedIndex = 0;
            foreach (TextStyle item in StyleList) AddToListView(item);
        }

        private void ctlStyleAdd_Click(object sender, EventArgs e)
        {
            TextStyle style = new TextStyle(Guid.NewGuid(), ctlFontName.SelectedItem.ToString(), ctlBold.Checked, ctlItalic.Checked, (byte)ctlTextSize.Value);
            if (StyleList.FirstOrDefault(item => item.FontName == style.FontName && item.Bold == style.Bold && item.Italic == style.Italic && item.FontSize == style.FontSize) != null)
            {
                MessageBox.Show("Bu stil zaten mevcut","Uyarı");
                return;
            }
            StyleList.Add(style);
            AddToListView(style);
        }

        private void AddToListView(TextStyle Style)
        {
            ListViewItem item = new ListViewItem();
            item.Tag = Style.Id;
            item.Text = Style.FontName;
            item.SubItems.Add(Style.Bold ? "Evet" : "Hayır");
            item.SubItems.Add(Style.Italic ? "Evet" : "Hayır");
            item.SubItems.Add(Style.FontSize.ToString());
            ctlStyleManagerListView.Items.Add(item);
        }

        private void ctlStyleDelete_Click(object sender, EventArgs e)
        {
            if (ctlStyleManagerListView.SelectedItems.Count < 1)
            {
                MessageBox.Show("Lütfen silmek istediğiniz stilleri seçiniz", "Uyarı");
                return;
            }
            foreach (ListViewItem item in ctlStyleManagerListView.SelectedItems)
            {
                ctlStyleManagerListView.Items.Remove(item);
                DeleteStyle((Guid)item.Tag);
            }
        }

        private void DeleteStyle(Guid guid)
        {
            StyleList.Remove(StyleList[guid]);
        }

        private void ctlOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void ctlStyleManagerListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ctlStyleManagerListView.SelectedItems.Count == 0) return;
            ListViewItem item = (ctlStyleManagerListView.SelectedItems.Count == 1) ? ctlStyleManagerListView.SelectedItems[0] : ctlStyleManagerListView.SelectedItems[ctlStyleManagerListView.SelectedItems.Count - 1];
            ShowItemInEditor(StyleList[(Guid)item.Tag]);
        }

        private void ShowItemInEditor(TextStyle item)
        {
            ctlFontName.SelectedIndex = ctlFontName.Items.IndexOf(item.FontName);
            ctlBold.Checked = item.Bold;
            ctlItalic.Checked = item.Italic;
            ctlTextSize.Value = item.FontSize;
        }
    }
}
